package com.base.forum.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.forum.dao.InvitationDAO;
import com.base.forum.entity.Forum;
import com.base.forum.entity.Invitation;
import com.base.forum.service.ForumService;
import com.base.forum.service.InvitationService;
import com.base.kongjian.entity.Comment;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.service.CommentService;
import com.base.kongjian.service.ReplyService;
import com.base.kongjian.service.WealthRecService;
import com.base.kongjian.service.WorkShareRecService;
import com.base.kongjian.service.WorksContributeRecService;
import com.base.kongjian.service.WorksNumCountService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.mysteel.page.Page;

@Service("invitationService")
public class InvitationServiceImpl implements InvitationService
{

	@Autowired
	private CommentService commentService;
	@Autowired
	private InvitationDAO invitationDao;
	@Autowired
	private WorksNumCountService worksNumCountService;
	@Autowired
	private WealthRecService wealthRecService;
	/*@Autowired
	private WealthSystemService wealthSystemService;*/
	@Autowired
	private WorkShareRecService workShareRecService;
	/*@Autowired
	private PopularitySystemService popularitySystemService;*/
	@Autowired
	private WorksContributeRecService worksContributeRecService;
	@Autowired
	private ReplyService replyService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ForumService forumService;
	
	@Override
	public void post(Invitation invitation)
	{
		Assert.notNull (invitation);
		long forumId = invitation.getForumId ();
		Forum forum = forumService.getById (forumId);
		
		if(ObjectUtils.notNull (forum))
		{
			invitation.setPublisherTime (System.currentTimeMillis ());
			Comment comment = new Comment ();
			comment.setWorkId (invitation.getId ());
			comment.setWorkKind (4);//作品种类1-微博,2-视频,3-音乐,4-帖子,5-照片
			comment.setMemberId (invitation.getPublisherId ());
			comment.setCommentTime (System.currentTimeMillis ());
			comment.setComment_MId (0L);
			comment.setContent ("--");
			invitationDao.save (invitation);
			commentService.comment (comment);
			worksNumCountService.updateWorksNumCount (invitation.getPublisherId () ,"invation" ,"add");
			forum.setNumOfInvatation (forum.getNumOfInvatation ()+1);
			
			forumService.update (forum);
		}
	}

	@Override
	public void deletePost(long id)
	{
		Invitation i  =  invitationDao.get (id);
		if(ObjectUtils.notNull (i))
		{
			long memberId = i.getPublisherId ();
			worksNumCountService.updateWorksNumCount (memberId ,"invation" ,"delete");
			Map<String,Object> params = new HashMap<String,Object>();
			params.put ("workId" ,id);
			params.put ("workKind" ,4);
			worksContributeRecService.deleteWCRByMap (params);
			workShareRecService.deleteWSRByMap (params);
			wealthRecService.deleteWRByMap (params);
			
			invitationDao.delete (id);
		}
	}

	@Override
	public void upate(Invitation invitation)
	{
		Assert.notNull (invitation);
		
		Invitation i = invitationDao.get (invitation.getId ());
		
		if(ObjectUtils.notNull (i))
		{
			i.setLastReplyId (invitation.getLastReplyId ());
			i.setLastReplyName (invitation.getLastReplyName ());
			i.setReadNum (invitation.getReadNum ());
			i.setReplyNum (invitation.getReplyNum ());
			i.setLastReplyTime (invitation.getLastReplyTime ());
			
			invitationDao.update (i);
		}
	}

	@Override
	public Invitation getById(long id)
	{
		Invitation i = invitationDao.get (id);
		if(ObjectUtils.notNull (i))
		{
			i.setReadNum (i.getReadNum ()+1);
			upate (i);
		}
		
		return i;
	}

	@Override
	public Page<Invitation> query(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<Invitation> page = null;
		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = invitationDao.count(params);
		if (count > 0)
		{
			page = new Page<Invitation>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);
			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> InvitationIds = invitationDao.query(params);

			if (ObjectUtils.notEmpty(InvitationIds))
			{
				List<Invitation> Invitations = new ArrayList<Invitation>(InvitationIds.size());

				for (long InvitationId : InvitationIds)
				{
					Invitations.add(getById (InvitationId));
				}

				page.setDatas(Invitations);
			}
		}

		return page;
	}

	@Override
	public void Replies(long id,Reply reply)
	{
		Invitation invitation = invitationDao.get (id);
		if(ObjectUtils.notNull (invitation))
		{
			Map<String, Object> params = new HashMap<String,Object> ();
			params.put ("workId" ,id);
			params.put ("workKind" ,4);
			
			Page<Comment> page = commentService.queryComment (params ,0 ,-1);
			if(ObjectUtils.notNull (page))
			{
				Comment comment = page.getDatas ().get (0);
				reply.setCommentId (comment.getId ());
				reply.setReplyTime (System.currentTimeMillis ());
				replyService.reply (reply);
				invitation.setLastReplyId (reply.getReply_MId ());
				Member member = memberService.getMemberById (reply.getReply_MId ());
				if(ObjectUtils.notNull (member))
				{
					invitation.setLastReplyName (member.getNickName ());
				}
				invitation.setReplyNum  (invitation.getReplyNum ()+1);
				invitation.setLastReplyTime (System.currentTimeMillis ());
				
				workShareRecService.addShareRec (id ,4 ,4 ,comment.getMemberId (),reply.getReply_MId ());
			
				upate (invitation);
			}
		}
	}
	
	
}
